package com.dynamitechetan.fogviewlibrary;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.BlendMode;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.render.Texture;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;
import ohos.multimodalinput.event.TouchEvent;

public class FogView extends Component implements Component.DrawTask, Component.TouchEventListener {
    private final String DEF_IMAGE = "fog_image";
    private final String DEF_STROKE_WIDTH = "stroke_width";

    private PixelMap bitmap = null;
    private PixelMap frontBitmap = null;
    private Path path;
    private Canvas mCanvas;
    private Paint paint;
    private int mIcon = ResourceTable.Media_fog_dense;
    private int strokeWidth = 75;
    private Context context;

    public FogView(Context context, AttrSet attrs) {
        super(context, attrs);
        this.context = context;
        boolean hasParam = attrs.getAttr(DEF_IMAGE).isPresent();
        if (hasParam) {
            String vlaue = attrs.getAttr(DEF_IMAGE).get().getStringValue();
            mIcon = Integer.parseInt(vlaue.split(":")[1]);
        }
        hasParam = attrs.getAttr(DEF_STROKE_WIDTH).isPresent();
        if (hasParam) {
            strokeWidth = attrs.getAttr(DEF_STROKE_WIDTH).get().getIntegerValue();
        }
        addDrawTask(this);
        setTouchEventListener(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (mCanvas == null) {
            eraseBitmp();
        }
        canvas.drawPixelMapHolder(new PixelMapHolder(bitmap), 0, 0, new Paint());
        mCanvas.drawPath(path, paint); // Draw a path
    }

    private void eraseBitmp() {
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        Size size = new Size(getWidth(), getHeight());
        options.pixelFormat = PixelFormat.ARGB_8888;
        options.size = size;
        bitmap = PixelMap.create(options);
        frontBitmap = ResUtil.preparePixelmap(context, mIcon);
        // Set the brush style
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE_STYLE); // Set the brush style: Hollow
        paint.setAntiAlias(true); // Anti according to tooth
        paint.setDither(true); // Anti jitter
        paint.setStrokeJoin(Paint.Join.ROUND_JOIN); // Set the joint appearance, ROUND arc
        paint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        paint.setStrokeWidth(strokeWidth); // Set the stroke width
        paint.setBlendMode(BlendMode.CLEAR);
        paint.clearBlurDrawLooper();
        path = new Path();
        mCanvas = new Canvas(new Texture(bitmap));
        RectFloat rectFloat = new RectFloat(0, 0, getRight(), getBottom());
        mCanvas.drawPixelMapHolderRect(new PixelMapHolder(frontBitmap), rectFloat, new Paint());
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent event) {
        float ax = event.getPointerScreenPosition(0).getX() - getLeft();
        float ay = event.getPointerScreenPosition(0).getY() - getLocationOnScreen()[1];
        if (event.getAction() == TouchEvent.PRIMARY_POINT_DOWN) {
            path.reset();
            path.moveTo(ax, ay);
            invalidate();
            return true;
        } else if (event.getAction() == TouchEvent.POINT_MOVE) {
            path.lineTo(ax, ay);
            invalidate();
            return true;
        }
        invalidate();
        return false;
    }
}